local UIBase = require("view.UIBase")
local PopupEquipment = class("PopupEquipment", UIBase)
local UIUtil = require("utils.UIUtil")
local TextHelper = require("utils.TextHelper")
local EquipDataHelper = require("data.helper.EquipDataHelper")

function PopupEquipment:ctor(pos)
    self._pos = pos
    local resource = {
        path = "Prefabs/Popup",
        name = "PopupEquipment",
        canvas = 3
    }
    PopupEquipment.super.ctor(self, resource)
end

function PopupEquipment:awake()
end

function PopupEquipment:start()
    UIUtil.addTouchEvent(self._panelTouch.transform, nil, nil, handler(self, self._onPanelTouchClick))
    UIUtil.addButtonClick(self._btnChange.transform, handler(self, self._onBtnChangeClick))
    UIUtil.addButtonClick(self._btnUpgrade.transform, handler(self, self._onBtnUpgradeClick))

    local equipUnit = G_UserData:getBattle():getBattleEquipment(self._pos)
    self._signalUpSuccess =
        G_SignalManager:add(SignalConst.EVENT_EQUIPMENT_UP_SUCCESS, handler(self, self._onEventUpSuccess))
    self:updateView(equipUnit)
end

function PopupEquipment:updateView(unit)
    if unit then
        self:_updateUI(unit)
    else
        self:_updateEmpty()
    end
end

function PopupEquipment:_updateEmpty()
    self._textEmpty.gameObject:SetActive(true)
    self._imageEquip.gameObject:SetActive(false)
    self._textTitle.text = Lang.get("none_equipment")
    self._textAttr1.text = ""
    self._textAttr2.text = ""
    self._textDesc.text = ""
end

function PopupEquipment:_updateUI(unit)
    self._unit = unit
    self._textEmpty.gameObject:SetActive(false)
    self._imageEquip.gameObject:SetActive(true)
    local config = self._unit:getConfig()
    self._imageEquip.sprite = G_ResourceManager:loadSprite("EquipAltas", config.res)
    self._textTitle.text = self._unit:getConfig().name
    self._textDesc.text = self._unit:getConfig().desc

    local mainAttr, subAttr = EquipDataHelper.getEquipmentAttr(self._unit)

    local mainName, mValue = TextHelper.getAttrBasicText(mainAttr.id, mainAttr.value)
    self._textAttr1.text = mainName .. "+" .. mValue
    local subName, sValue = TextHelper.getAttrBasicText(subAttr.id, subAttr.value)
    self._textAttr2.text = subName .. "+" .. sValue
end

function PopupEquipment:onClose()
    self._signalUpSuccess:remove()
    self._signalUpSuccess = nil
end

function PopupEquipment:_onEventUpSuccess(id, mess)
    self:_updateUI(self._unit)
end

function PopupEquipment:_onBtnChangeClick()
    local pop = G_UIManager:openPopupView("PopupChooseEquipment")
    pop:setCallback(handler(self, self._onEquipment))
    if self._unit then
        pop:setEquipId(self._unit:getId())
    end
end

function PopupEquipment:_onEquipment(equipId)
    if equipId and equipId > 0 then
        G_UserData:getBattle():addEquipmentToBattle(self._pos, equipId)
        local equipUnit = G_UserData:getBattle():getBattleEquipment(self._pos)
        self:updateView(equipUnit)
    end
end

function PopupEquipment:_onBtnUpgradeClick()
    local pop = G_UIManager:openPopupView("PopupEquipmentUp")
    pop:updateUI(self._unit)
end

function PopupEquipment:_onPanelTouchClick()
    self:close()
end

return PopupEquipment
